package com.speily.javabase.jdk8;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * @Auther: SPL
 * @Date: 2019-07-18 14:24
 * @Description: - java 8 新特性
 */
public class JDK8Test {


    public static void main(String[] args) {


        List<String> list = new ArrayList<>(Arrays.asList("biluo", "huangquan", "hongchen", "zimo"));
        Function<String, String> function = String::toUpperCase;
        list.stream()
                .map(function)
                .forEach(System.out::println);


    }

    /**
     * 折扣优惠计算方法
     */
    private static void studentFreefun() {
        Student student1 = new Student("Ashok","Kumar", 8);

        //不同年领不同优惠,8岁以下打6折，8-16打8折,16岁以上不打折

        student1 = updateStudentFee(student1,
                s -> s.age <= 8,
                s -> s.feeDiscount = 0.6);
        student1.printFee();

        Student student2 = new Student("Rajat","Verma", 15);
        student2 = updateStudentFee(student2,
                s -> s.age > 8 && s.age <=16,
                s -> s.feeDiscount = 0.8);
        student2.printFee();

        Student student3 = new Student("Rajat","Verma", 15);
        student3 = updateStudentFee(student3,
                s -> s.age > 8 && s.age <=16,
                s -> s.feeDiscount = 1d);
        student3.printFee();

    }

    /**
     * @param student 对象
     * @param predicate 断言函数
     * @param consumer 消费函数
     * @return
     */
    public static Student updateStudentFee(Student student, Predicate<Student> predicate, Consumer<Student> consumer){
        if (predicate.test(student)){
            consumer.accept(student);
        }
        return student;
    }


    private static class UserT {
        public UserT(String name) {
            this.name = name;
        }
        private String name;
    }

    private static class Student {
        private String firstName;
        private String lastName;
        int age;//年级
        Double feeDiscount = 0.0;//优惠
        Double baseFee = 100.0;//基础费

        public Student(String firstName, String lastName, int age) {
            this.firstName = firstName;
            this.lastName = lastName;
            this.age = age;
        }

        public void printFee(){
            Double newFee = baseFee - ((baseFee * (1-feeDiscount)));
            System.out.println("去掉优惠应付金额: " + newFee);
        }
    }

    @Test
    public void  StrTest(){
        String value = "12342423.777";
        value = value.replaceAll("\\d"," ").replaceAll("."," ");
        System.out.println("########"+ value +"###########");
    }
}
